package com.tich.program.listener;

import cn.hutool.core.bean.BeanUtil;
import com.tich.constant.RabbitMqConstant;
import com.tich.program.domain.entity.TicketCategory;
import com.tich.program.domain.entity.TicketCategoryDeleted;
import com.tich.program.mapper.TicketCategoryDeletedMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class TicketCategoryListener {

    @Autowired
    private TicketCategoryDeletedMapper ticketCategoryDeletedMapper;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = RabbitMqConstant.TICKET_CATEGORY_DELETE_QUEUE),
            exchange = @Exchange(name = RabbitMqConstant.PROGRAM_DIRECT, type = ExchangeTypes.DIRECT),
            key = {RabbitMqConstant.TICKET_CATEGORY_DELETE_KEY}
    ))
    public void listenTicketCategoryDelete(List<TicketCategory> ticketCategories){
        log.info("将已删除的门票类型添加到删除表中：{}", ticketCategories);
        List<TicketCategoryDeleted> ticketCategoryDeleteds = BeanUtil.copyToList(ticketCategories, TicketCategoryDeleted.class);
        ticketCategoryDeletedMapper.insert(ticketCategoryDeleteds);

    }

}
